package com.jz.community.moduleshopping.cardGoods.adapter;

import android.app.Activity;
import android.support.annotation.Nullable;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jz.community.basecomm.glideConfig.GlideApp;
import com.jz.community.basecomm.utils.BaseImageLoaderUtils;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.cardGoods.info.CardHomeInfo;

import java.util.List;

/**
 * Created by Tony.Fan on 2018/8/16 11:01
 */
public class CardClassifyGoodsAdapter extends BaseQuickAdapter<CardHomeInfo.EmbeddedBeanXX.ContentBean.CardGoodsInfo, BaseViewHolder> {

    private float saleImageRadio = 144.0f / 144.0f;

    private int imageWidth;
    private int imageHeight;

    public CardClassifyGoodsAdapter(@Nullable List<CardHomeInfo.EmbeddedBeanXX.ContentBean.CardGoodsInfo> data) {
        super(R.layout.module_shopping_card_classify_item_layout, data);

    }

    @Override
    protected void convert(BaseViewHolder helper, CardHomeInfo.EmbeddedBeanXX.ContentBean.CardGoodsInfo item) {
        imageWidth = ((SHelper.getScrrenWidth((Activity) mContext) - SHelper.dp2px(mContext, 30)) / 2) - SHelper.dp2px(mContext, 26);
        imageHeight = (int) (imageWidth / saleImageRadio);
        ImageView item_card_classify_iv = helper.getView(R.id.item_card_classify_iv);
        RelativeLayout rl = helper.getView(R.id.item_card_classify_rl);
        TextView no_sku = helper.getView(R.id.item_card_classify_no);

        ViewGroup.LayoutParams lp_rl = rl.getLayoutParams();
        lp_rl.width = imageHeight;
        lp_rl.height = imageWidth;
        rl.setLayoutParams(lp_rl);


        ViewGroup.LayoutParams lp_img = item_card_classify_iv.getLayoutParams();
        lp_img.height = imageHeight;
        lp_img.width = imageWidth;

        item_card_classify_iv.setLayoutParams(lp_img);

        if (item.getStock() != 0) {
            SHelper.gone(no_sku);
        } else {
            SHelper.vis(no_sku);
        }

        if (mData.size() > 9) {
            if (helper.getAdapterPosition() == mData.size() - 1) {
                GlideApp.with(mContext).load(item.getLoadMoreRes()).into(item_card_classify_iv);
            }
        } else {
            BaseImageLoaderUtils.getInstance().loadRamdomImage(mContext, item_card_classify_iv, item.getImages());
        }
    }
}
